var currPage; var thePDF = null; function handlePages(page, fileName) { // you can now use *page* here var scale = 1.5; var viewport = page.getViewport(scale); var canvas = document.getElementById('the-canvas'); var context = canvas.getContext('2d'); canvas.height = viewport.height; canvas.width = viewport.width; var renderContext = { canvasContext: context, viewport: viewport }; page.render(renderContext).promise.then(function () { var canvas = document.getElementById('the-canvas'); var fileObject = canvas.toDataURL('image/ + png + ').replace('image/ + png + ', 'image/octet-stream'); //Convert image to 'octet-stream' (Just a download, really) var outputFile = fileName.toLowerCase().replace('.pdf', '') + (thePDF.numPages > 1 ? (' page ' + (currPage)) : '') + '.png'; add_file_output(fileObject, outputFile, thePDF.numPages === 1); //Move to next page currPage++; if (thePDF !== null && currPage <= thePDF.numPages) { thePDF.getPage(currPage).then(function (page) { handlePages(page, fileName); }); } }); } function fireEvent(obj, evt) { var fireOnThis = obj; if (document.createEvent) { var evObj = document.createEvent('MouseEvents'); evObj.initEvent(evt, true, false); fireOnThis.dispatchEvent(evObj); } else if (document.createEventObject) { var evObj = document.createEventObject(); fireOnThis.fireEvent('on' + evt, evObj); } } function processFile(blob, fileName) { var reader = new FileReader(); reader.onload = function (e) { PDFJS.workerSrc = '/js/pdf.worker.js'; var pdfAsDataUri = e.target.result; var pdfAsArray = convertDataURIToBinary(pdfAsDataUri); PDFJS.getDocument(pdfAsArray).then(function (pdf) { thePDF = pdf; pdf.getPage(1).then(function (page) { currPage = 1; handlePages(page, fileName); }); }); } reader.readAsDataURL(blob); var BASE64_MARKER = ';base64,'; function convertDataURIToBinary(dataURI) { var base64Index = dataURI.indexOf(BASE64_MARKER) + BASE64_MARKER.length; var base64 = dataURI.substring(base64Index); var raw = window.atob(base64); var rawLength = raw.length; var array = new Uint8Array(new ArrayBuffer(rawLength)); for (i = 0; i < rawLength; i++) { array[i] = raw.charCodeAt(i); } return array; } } function replaceAll(find, replace, str) { return str.replace(new RegExp(find, 'g'), replace); } function beautify(str) { var result = ''; var length = str.length; var i = 0; var braceCountLeft = 0; var braceCountRight = 0; var withinQuotes = false; while (i < length) { var c = str[i]; if (c == '"' && (i == 0 || c[i - 1] != '\\')) { // non-escaped quotes withinQuotes = !withinQuotes; } if (!withinQuotes && (c == '}' || c == '{' || c == ',')) { console.log('Start####' + result); // look back and remove carriage returns and whitespace that are already there var resultIndex = result.length - 1; while (resultIndex >= 0 && (result[resultIndex] == ' ' || result[resultIndex] == '\r' || result[resultIndex] == '\n' || result[resultIndex] == '\t')) { resultIndex = resultIndex - 1; result = result.substr(0, resultIndex + 1); console.log('char ' + result[resultIndex] + '-----' + result + 'zzz ' + result.length + ' ' + resultIndex); } if (c == '{') { braceCountLeft++; result += c + '\r' + GetTabs(braceCountLeft - braceCountRight); } else if (c == '}') { braceCountRight++; // precede with carriage return result += '\r' + GetTabs(braceCountLeft - braceCountRight) + c; } else if (c == ',') { result += c + '\r' + GetTabs(braceCountLeft - braceCountRight); } var nextChar = ''; // advance through whitespace and remove carriage returns that are already there while (i < length && (str[i + 1] == ' ' || str[i + 1] == '\r' || str[i + 1] == '\n' || str[i + 1] == '\t')) { i++; } } else { result += str[i]; } i++; } return result; } function GetTabs(count) { var result = ''; for (var i = 0; i < count; i++) { result += ' '; } return result; }